
import AbsMenu from '../absmenu.js'
import { LGXEnums } from '../../../canvas/common/sysenum.js';


class EditMenu extends AbsMenu.AbsMenu {
  constructor(map, item) {
    super(map, item);
    this.subItemRelateValueList = [];
  }

  act2Click() {
    let rst = null;
    console.log("eidt-----+" + this.menuSubItem);
    switch (this.menuSubItem) {
      case 'clear':
        rst = this.clear();
        break;
      case 'select':
        rst = this.pickobj();
        break;
      case 'undo':
        rst = this.undo();
        break;
      case 'redo':
        rst = this.redo();
        break;

      case 'copy':
        rst = this.map.copy();
        break;

      case 'cut':
        rst = this.cut();
        break;

      case 'paste':
        this.map.paste();
        break;

      case 'rightRotate':
        rst = this.rotate(-30);
        break;

      case 'leftRotate':
        rst = this.rotate(30);
        break;

      case 'RoundChamfer':
        rst = this.RoundChamfer();
        break;

      case 'RectChamfer':
        rst = this.RectChamfer();
        break;

      case 'trim':
        rst = this.trim();
        break;

      case 'delete':
        rst = this.delete();
        break;

      case 'offsetCopy':
        rst = this.offsetCopy();
        break;

      case 'mirror':
        rst = this.mirror();
        break;

      case 'rotate':
        rst = this.dynamicRotate();
        break;

      case 'extend':
        rst = this.extend();
        break;

      case 'ringArray':
        rst = this.ringArray();
        break;

      case 'rectArray':
        rst = this.rectArray();
        break;

      case 'translate':
        rst = this.translate();
        break;

      case 'fill':
        rst = this.fill();
        break;

      case 'circletangency':
        rst = this.circletangency();
        break;

      case 'fastDelete':
        rst = this.fastDelete();
        break;

      case 'scale':
        rst = this.scale();
        break;

      case 'panView':
        rst = this.panView();
        break;

      default:
        break;
    }

    this.preSubMenu = this.findPreItem(this.menuSubItem);
    return rst;
  }

  clear() {
    this.map.clearData();
    let rst = { nextTips: "", appendToPreCMD: "clear" };
    return rst;
  }

  pickobj() {
    this.map.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
    let rst = { nextTips: "鼠标左键按下选中图中图形", appendToPreCMD: "pick" };
    return rst;
  }

  undo() {
    this.map.undo();
    let rst = { nextTips: "", appendToPreCMD: "undo" };
    return rst;
  }
  redo() {
    this.map.redo();
    let rst = { nextTips: "", appendToPreCMD: "redo" };
    return rst;
  }

  cut() {
    console.log("cut ");
    let rst = { nextTips: "", appendToPreCMD: "cut" };
    return rst;
  }

  delete() {
    this.map.m_pEditINTF.delete();
    let rst = { nextTips: "", appendToPreCMD: "delete" };
    return rst;
  }

  fastDelete() {
    this.map.setEditMode(LGXEnums.EditMode.fastDelete);
    let rst = { nextTips: "左键选中图形，快捷删除", appendToPreCMD: "delete" };
    return rst;
  }



  rotate(degree) {
    let preItem = this.findPreItem(this.menuSubItem);
    if (preItem) {
      degree = parseFloat(preItem.value);
    }
    else {
      this.updateItemRelateValue(this.menuSubItem, degree);
    }
    this.map.m_pEditINTF.rotate(degree);
    let msg = "rotate," + degree;
    let rst = { nextTips: "", appendToPreCMD: msg };
    return rst;
  }

  RoundChamfer() {
    let radius = 10;
    let preItem = this.findPreItem(this.menuSubItem);
    if (preItem) {
      radius = parseFloat(preItem.value);
    }
    else {
      this.updateItemRelateValue(this.menuSubItem, radius);
    }

    this.map.setEditMode(LGXEnums.EditMode.roundChamfer);
    let tips = "选中两条相交线交点或输入圆角半径(" + radius + "),结束按Esc";
    let rst = { nextTips: tips, appendToPreCMD: "roundchamfer" };
    return rst;
  }

  RectChamfer() {
    let lenth = 10;
    let preItem = this.findPreItem(this.menuSubItem);
    if (preItem) {
      lenth = parseFloat(preItem.value);
    }
    else {
      this.updateItemRelateValue(this.menuSubItem, lenth);
    }
    let tips = "输入切边长度(" + lenth + ")";
    this.map.setEditMode(LGXEnums.EditMode.rectChamfer);
    let rst = { nextTips: tips, appendToPreCMD: "rectchamfer" };
    return rst;
  }

  trim() {
    this.map.setEditMode(LGXEnums.EditMode.trim);
    let rst = { nextTips: "选中图形修剪", appendToPreCMD: "trim" };
    return rst;
  }

  fill() {
    this.map.setEditMode(LGXEnums.EditMode.fill);
    let rst = { nextTips: "选中封闭区域填充", appendToPreCMD: "fill" };
    return rst;
  }

  offsetCopy() {
    let offsetGap = 10;
    let preItem = this.findPreItem(this.menuSubItem);
    if (preItem) {
      offsetGap = parseFloat(preItem.value);
    }
    else {
      this.updateItemRelateValue(this.menuSubItem, offsetGap);
    }

    let tips = "选中图形进行偏移或输入其他偏移距离（" + offsetGap + ")";
    this.map.setEditMode(LGXEnums.EditMode.offsetCopy);
    let rst = { nextTips: tips, appendToPreCMD: "offset" };
    return rst;
  }

  mirror() {
    this.map.setEditMode(LGXEnums.EditMode.mirror);
    let rst = { nextTips: "鼠标左键按下或输入选中镜像轴第一个坐标点（x,y）,取消（Esc）", appendToPreCMD: "mirror" };
    return rst;
  }

  dynamicRotate() {
    this.map.setEditMode(LGXEnums.EditMode.rotate);
    let rst = { nextTips: "鼠标左键按下或输入坐标（x,y）确定旋转中心点,取消（Esc）", appendToPreCMD: "rotate" };
    return rst;
  }

  extend() {
    let rst = this.map.setEditMode(LGXEnums.EditMode.extend);
    if (!rst) {
      rst = { nextTips: "鼠标左键按下选中要延长的线段。取消（Esc）。确定直接回车（e）", appendToPreCMD: "extend" };
    }
    return rst;
  }

  translate() {
    this.map.setEditMode(LGXEnums.EditMode.translate);
    let rst = { nextTips: "鼠标左键按下或输入确定移动基点（x,y）,取消（Esc）", appendToPreCMD: "translate" };
    return rst;
  }

  circletangency() {
    this.map.setEditMode(LGXEnums.EditMode.circletangency);
    let rst = { nextTips: "鼠标左键按下选中第一个圆,取消（Esc）", appendToPreCMD: "circletangency" };
    return rst;
  }

  setPreMenuRelateValue(value) {
    let rst = null;
    if (this.preSubMenu) {
      this.preSubMenu.value = value;
      rst = this.map.setCurentEditModeRelateValue(value);
    }
    return rst;
  }

  updateItemRelateValue(item, value) {
    let preItem = this.findPreItem(item);
    if (!preItem) {
      preItem = new AbsMenu.ItemRelateValud(item);
      this.subItemRelateValueList.push(preItem);
    }
    preItem.value = value;
    return preItem;
  }

  findPreItem(item) {
    let dstMenu = null;
    for (let i = 0; i < this.subItemRelateValueList.length; i++) {
      let t = this.subItemRelateValueList[i];
      if (t.item == item) {
        dstMenu = t;
        break;
      }
    }
    return dstMenu;
  }

  ringArray() {
    let t = window.circleArrayDlg.value;
    if (t && !t.visible) {
      t.visible = true;
    }
  }

  rectArray() {
    let t = window.rectArrayDlg.value;
    if (t && !t.visible) {
      t.visible = true;
    }
  }

  scale() {
    let rst = this.map.setEditMode(LGXEnums.EditMode.scale);
    if (!rst) {
      rst = { nextTips: "鼠标左键按下或输入确定移动基点（x,y）,取消（Esc）", appendToPreCMD: "scale" };
    }
    return rst;
  }

  panView(){
    this.map.setMapWorkMode(LGXEnums.LGXWorkMode.WanderMode);
    let rst = { nextTips: "拖动画布移动", appendToPreCMD: "pan" };
    return rst;
  }
}

export default { EditMenu }

